<?php # AccountInformation.php
session_start();
require "customers.class.php";
require "address.class.php";

/*
 * Specify the field names that are in the session.
 */
$allowedFields = array(
 			'customerId',
			'user_name'
);

/*
 * Specify the field names that are required in the session
 */
$requiredFields = array(
			'customerId'
);

$error = false;
$result = '';
$error_array = array();
$return_array = array();
$account_array = array();
foreach($_SESSION AS $key => $value)
{
	// first make sure this is an allowed field
	if(in_array($key, $allowedFields))
	{
		$$key = $value;
		
		// is this a required field?
		if(in_array($key, $requiredFields) && $value == '')
		{
			$error = true;
			array_push($error_array, 'The field '.$key.' is required.');
		}
	}
	else
	{
		$error = true;
		array_push($error_array, 'The field '.$key.' does not exist.');
	}
}

if (!$error) {
	$customerId = '';
	if (isset($_SESSION['customerId'])){
		$customerId = $_SESSION['customerId'];
	}
	else {
		$customerId = '';
	}
	
	// check to make sure that the customer exists.
	$customer = Customers::loadUnique($customerId);
	$address = $customer->getAddress();
  	if($customer->getCustomerId() == $customerId) {
		//*** Update Condition ***//
		if(isset($_POST['Action'])) {
			if($_POST["Action"] == "Update") {		
				if (isset($_POST['Title'])){
					if(strlen($_POST['Title']) > 10) {
						$error = true;
						array_push($error_array, 'Title does not allow strings longer than 10 characters.');
					}
					else {
						$customer->setCustomerTitle($_POST['Title']);
					}
				}
				if (isset($_POST['LastName'])){
					if(strlen($_POST['LastName']) > 45) {
						$error = true;
						array_push($error_array, 'Last Name does not allow strings longer than 45 characters.');
					}
					else {
						$customer->setCustomerLastName($_POST['LastName']);
					}
				}
				if (isset($_POST['FirstName'])){
					if(strlen($_POST['FirstName']) > 45) {
						$error = true;
						array_push($error_array, 'First Name does not allow strings longer than 45 characters.');
					}
					else {
						$customer->setCustomerFirstName($_POST['FirstName']);
					}
				}
				if (isset($_POST['DOB'])){
					$customer->setCustomerDOB($_POST['DOB']);
				}
				if (isset($_POST['HomePhone'])){
					if(strlen($_POST['HomePhone']) > 20) {
						$error = true;
						array_push($error_array, 'Home Phone does not allow strings longer than 20 characters.');
					}
					else {
						$customer->setCustomerHomePhone($_POST['HomePhone']);
					}
				}
				if (isset($_POST['MobilePhone'])){
					if(strlen($_POST['MobilePhone']) > 20) {
						$error = true;
						array_push($error_array, 'Mobile Phone does not allow strings longer than 20 characters.');
					}
					else {
						$customer->setCustomerMobilePhone($_POST['MobilePhone']);
					}
				}
				if (isset($_POST['EmailAddress'])){
					if(strlen($_POST['EmailAddress']) > 45) {
						$error = true;
						array_push($error_array, 'Email Address does not allow strings longer than 45 characters.');
					}
					else if(!filter_var($_POST['EmailAddress'], FILTER_VALIDATE_EMAIL)) {
						$error = true;
						array_push($error_array, 'Please provide a valid email address.');
					}
					else {
						$customer->setCustomerEmail($_POST['EmailAddress']);
					}
				}
				if (isset($_POST['Password'])){
					if(strlen($_POST['Password']) > 45) {
						$error = true;
						array_push($error_array, 'Password does not allow strings longer than 45 characters.');
					}
					else if($_POST['Password'] != $_POST['PasswordConfirm']) {
						$error = true;
						array_push($error_array, 'The 2 passwords you have entered do not match.');
					}
					else {
						$customer->setPassword($_POST['Password']);
					}
				}
				if(isset($_POST['StreetAddress'])) {
					if(strlen($_POST['StreetAddress']) > 100) {
						$error = true;
						array_push($error_array, 'Street Address does not allow strings longer than 100 characters.');
					}
					else {
						$address->setStreet($_POST['StreetAddress']);
					}
				}
				if(isset($_POST['City'])) {
					if(strlen($_POST['City']) > 45) {
						$error = true;
						array_push($error_array, 'City does not allow strings longer than 45 characters.');
					}
					else {
						$address->setCity($_POST['City']);
					}
				}
				if(isset($_POST['StateOrProvince'])) {
					if(strlen($_POST['StateOrProvince']) > 45) {
						$error = true;
						array_push($error_array, 'StateOrProvince does not allow strings longer than 45 characters.');
					}
					else {
						$address->setStateOrProvince($_POST['StateOrProvince']);
					}
				}
				if(isset($_POST['ZIP'])) {
					if(strlen($_POST['ZIP']) > 20) {
						$error = true;
						array_push($error_array, 'ZIP Code does not allow strings longer than 20 characters.');
					}
					else {
						$address->setPostalCode($_POST['ZIP']);
					}
				}
		
				if(!$error) {
					$address->update();
					$customer->update();
					$result = 'Success';
					$return_array['message'] = $result;
				}
				else {
					$return_array['error'] = $error_array;
					print json_encode($return_array);
				}
			}
		}
		
		// search for the account information for the customer logged into the session.
		$customerTitle = $customer->getCustomerTitle();
		$customerLastName = $customer->getCustomerLastName();
		$customerFirstName = $customer->getCustomerFirstName();
		$customerDOB = $customer->getCustomerDOB();
		$customerHomePhone = $customer->getCustomerHomePhone();
		$customerMobilePhone = $customer->getCustomerMobilePhone();
		$customerEmail= $customer->getCustomerEmail();
		$customerPassword = $customer->getPassword();
		$streetAddress = $address->getStreet();
		$city = $address->getCity();
		$stateOrProvince = $address->getStateOrProvince();
		$postalCode = $address->getPostalCode();
		$account_array['title'] = $customerTitle;
		$account_array['lastName'] = $customerLastName;
		$account_array['firstName'] = $customerFirstName;
		$account_array['dob'] = $customerDOB;
		$account_array['homePhone'] = $customerHomePhone;
		$account_array['mobilePhone'] = $customerMobilePhone;
		$account_array['email'] = $customerEmail;
		$account_array['password'] = $customerPassword;
		$account_array['streetAddress'] = $streetAddress;
		$account_array['city'] = $city;
		$account_array['stateOrProvince'] = $stateOrProvince;
		$account_array['postalCode'] = $postalCode;
		$return_array['customer'] = $account_array;
		print json_encode($return_array);
	}
	else {
		$error = true;
		array_push($error_array, 'Customer does not exist, so account information cannot be updated.');
		$return_array['error'] = $error_array;
		print json_encode($return_array);
	}
}
?>
